package com.gientecher.consulconsumer.service;

import com.gientecher.consulconsumer.config.RegisterConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class RegisterService {

    @Autowired
    private RegisterConfig registerConfig;

    @Autowired
    private RestTemplate restTemplate;

    public void doRegister(){
        String url = "http://"+registerConfig.getServerHost()+":"+registerConfig.getServerPort()+"/register";

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);

        Map<String,String> requestMap = new HashMap<>();
        requestMap.put("serviceName", registerConfig.getServiceName());
        requestMap.put("serviceAddress", registerConfig.getServiceAddress());
        requestMap.put("healthCheckUrl", registerConfig.getHealthCheckUrl());

        HttpEntity<Map<String,String>> entity = new HttpEntity<>(requestMap, headers);

        restTemplate.postForObject(url, entity,Object.class);

    }

}
